package cn.com.utt.unms.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.com.utt.unms.pojo.SmsMarketing;

@Mapper
public interface SmsMarketMapper {
	@Select({"<script>",
	    "SELECT count(*) as total ", 
	    "FROM sms_marketing ",
	    "WHERE user_id=#{userId}",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	      "limit #{start},#{rows}",
	    "</script>"})
	int countByCondition(Map<String,Object> conditions);
	
	@Select({"<script>",
	    "SELECT *", 
	    "FROM sms_marketing ",
	    "WHERE user_id=#{userId}",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	      "limit #{start},#{rows}",
	    "</script>"})
	List<SmsMarketing> queryByCondition(Map<String,Object> conditions);
	@Select("select * from sms_marketing where user_id=#{userId}")
	SmsMarketing querySmsMarketByUserId(@Param("userId") Long userId);
	@Insert("insert into sms_marketing(user_id,method,num,title,context,status,remark,create_time,sms_sign,sms_volume) values(#{userId},#{method},#{num},#{title},#{context},#{status},#{remark},#{createTime},#{smsSign},#{smsVolume})")
	@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
	void addSmsMarketing(SmsMarketing smsMarketing);
	@Select("select * from sms_marketing where id=#{id}")
	SmsMarketing querySmsMarketById(@Param("id") Long id);
	@Update("update sms_marketing set title=#{title},contxt=#{context} where id=#{id}")
	void updateSmsMarketById(@Param("title") String title,@Param("context") String context,@Param("id") Long id);
//	@Delete("delete from sms_marketing where id=#{id}")
//	void deleteSmsmarketById(@Param("id") Long id);
	@Update("update sms_marketing set num=#{num},title=#{title},context=#{context},status=#{status},remark=#{remark} where id=#{id}")
	void updateSmsMarketing(SmsMarketing smsMarketing);
	
	@Select({"<script>",
	    "select * from  sms_marketing", 
	    "WHERE ", 
	    "id IN", 
	      "<foreach item='item' index='index' collection='id'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	List<SmsMarketing> querySmsMarketByIds(@Param("id") List<Long> id);
	
	@Delete({"<script>",
	    "delete from sms_marketing", 
	    "WHERE ", 
	    "id IN", 
	      "<foreach item='item' index='index' collection='ids'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	void deleteSmsmarketByIds(@Param("ids") List<Long> ids);
	
	@Select("select * from sms_marketing where status=#{status}")
	List<SmsMarketing> querySmsMarketByStatus(@Param("status") Byte status);

}
